# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::SNMP::Snmp Snmp.pm
# Description:
#  - Requetes SNMP V2/V3 
#
# ======================================================================

package ADN::LIBRARY::SNMP::Snmp;

use strict;
use Net::SNMP qw(:snmp);
use ADN::CONF::Read;

# #######################
# Création de l'objet self + init. variables
# #######################
sub new
{
  	# Construction de l'objet $self
	  my $self = shift;
	  my $class = ref($self) || $self;
	  return $self if ref $self;
	  $self = bless {} => $class;
    ($self->{BASE_OID}, $self->{SYSTEM}) = @_;
    if ($self->_init() =~ m/^[-]1$/smx)
    {
      return -1;
    }
    return $self;
}

# #######################
# Fonction initialisation
# #######################
sub _init {
        my $self = shift;
  # Chargement du fichier de configuration XML + Initialisation variable.
  my $conf = ADN::CONF::Read->new(); if ($conf =~ m/^[-]1$/smx) {
    return -1;
  } my %SNMP = $conf->SNMP(); $self->{repete} = $SNMP{maxrepet};
        undef $conf; undef %SNMP; return;
}

# #######################
# Fonction principale
# #######################
sub start
{
  my $self = shift;
  ($self->{session}) = @_;
  my @arg;

  $arg[0] = $self;
  $self->{session}->get_bulk_request(
          -callback       => [\&base_oid, @arg],
          -maxrepetitions => $self->{repete},
          -varbindlist    => [$arg[0]->{BASE_OID}] );

  snmp_dispatcher();
  $self->{session}->close;
  return (\%{$arg[0]->{result}});
}

# #######################
# Fonction callback
# #######################
sub base_oid
{
    my ($session, @arg) = @_;
    my %system = %{$arg[0]->{SYSTEM}};
    my ($next, $oid, $key, $boucle);

    if (defined $session->var_bind_list)
    {
	     foreach $oid (oid_lex_sort(keys %{$session->var_bind_list}))
	     {
	       if (!oid_base_match($arg[0]->{BASE_OID}, $oid))
	       {
		        $next = undef;
		        last;
	       }
	       $next = $oid;
	       $arg[1]->{$oid} = $session->var_bind_list->{$oid};
	     }
	     if (defined $next)
	     {
	           $session->get_bulk_request(
		            -callback       => [\&base_oid, @arg],
		            -maxrepetitions => $arg[0]->{repete},
		            -varbindlist    => [$next] );
	     }
	     else
	     {
	        foreach $key (keys %system)
	        {
            $arg[0]->{result}{$key} = $arg[1]->{$system{$key}};
            if (!defined $arg[1]->{$system{$key}})
            {
              $boucle = 1;
              foreach $oid (oid_lex_sort(keys %{$arg[1]}))
              {
                if (($oid =~ m/^($system{$key})\./smx) && ($arg[1]->{$oid} !~ m/^$/smx))
                {
                  $arg[0]->{result}{$key.'.'.$boucle} = $arg[1]->{$oid};
                  $boucle++;
                }
              }
              $boucle--;
              $arg[0]->{result}{$key} = $boucle;
            }
          }
          $arg[0]->{result}{Erreur} = '0';
	    }
    }
    else
    {
      $arg[0]->{result}{Erreur} = '1';
    }
    undef $next;
		undef $oid;
		undef $key;
		undef $boucle;
    return $arg[0];
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__